package com.naiterui.ehp.bs.pc.modules.sys.utils;

import java.util.LinkedHashMap;
import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.naiterui.ehp.bs.pc.common.utils.ConfigUtil;
import com.naiterui.ehp.bs.pc.modules.sys.vo.DictVO;

/**
 * 字典工具类
 */
public class DictUtil {

    public static List<DictVO> queryDict(String type) {
        LinkedHashMap dict = ConfigUtil.bindProperties(type.replaceAll("_", "-"), LinkedHashMap.class);
        List<DictVO> list = Lists.newArrayList();
        if (dict == null || dict.isEmpty()) {
            return list;
        }
        dict.forEach((key, value) -> list.add(new DictVO(key.toString(), value.toString())));
        return list;
    }

    public static String getNameByTypeAndCode(String type, String code) {
        Preconditions.checkNotNull(type, "字典类型不能为null");
        Preconditions.checkNotNull(code, "字典编码不能为null");
        List<DictVO> list = DictUtil.queryDict(type);
        for (DictVO dictVO : list) {
            if (dictVO.getCode().equals(code)) {
                return dictVO.getValue();
            }
        }
        return String.format("没有查询到此字典编码type: %s code: %s", type, code);
    }

}
